﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework.Utils;
using CNVP.JShop.BLL;
using CNVP.Config;

namespace CNVP.JShop.Web
{
    public partial class Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.AddHeader("pragma", "no-cache");
            Response.AddHeader("cache-control", "");
            Response.CacheControl = "no-cache";
            var action = Request.QueryString["action"];
            switch (action)
            {
                case "getProductDetail":
                    var standard = Request.QueryString["standard"];
                    var pid = Request.QueryString["pid"];
                    if (string.IsNullOrEmpty(pid) || !Public.IsNumber(pid))
                    {
                        pid = "0";
                    }
                    Model.CNVP_ProductsDetail model= new BLL.CNVP_ProductsDetail().GetSingle_CNVP_ProductsDetailByPIDAndSID(Convert.ToInt32(pid),standard);
                    if (model == null)
                    {
                        Response.Write("{\"error\":\"无数据。\"}");
                    }
                    else
                    {
                        Response.Write("{\"id\":" + model.ID + ",\"price\":" + model.SalePrice + ",\"stocknum\":" + model.StockNum + "}");
                    }
                    break;
                case "AddToShoppingCart":
                    #region 添加到购物车 写入Cookies
                    var Amount = Request.Form["BuyCount"];
                    var ProductIDS = Request.Form["ProductIDS"];
                    var ProDetailIDS = string.IsNullOrEmpty(Request.Form["ProDetailIDS"]) ? "0" : Request.Form["ProDetailIDS"];
                    int sum = 0;
                    var AllCount = Request.Form["AllCount"];
                    var StockTotalNum = Request.Form["StockTotalNum"];
                    if (!string.IsNullOrEmpty(AllCount))
                    {
                        sum = Convert.ToInt32(AllCount);
                    }
                    else
                    {
                        sum = Convert.ToInt32(StockTotalNum);
                    }
                    try
                    {
                        if (int.Parse(Amount) > sum)
                        {
                            Response.Write("{\"error\":\"库存量不足。\",\"result\":\"-1\"}");
                            break;
                        }
                    }
                    catch
                    {
                        Response.Write("{\"error\":\"请先选择规格。\",\"result\":\"-1\"}");
                        break;
                    }


                    string sProductID = "";
                    string sProDetailID = "";
                    string sAmount = "";
                    if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"] != null)
                    {

                        if (ProDetailIDS != "0")
                        {
                            int j = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"].IndexOf(ProDetailIDS);
                            if (j < 0)
                            {
                                //产品主表IDS
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"] != "")
                                {
                                    sProductID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"] + "," + ProductIDS;
                                }
                                else
                                {
                                    sProductID = ProductIDS;
                                }
                                //详细表IDS
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"] != "")
                                {
                                    sProDetailID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"] + "," + ProDetailIDS;
                                }
                                else
                                {
                                    sProDetailID = ProDetailIDS;
                                }
                                //数量
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"] != "")
                                {
                                    sAmount = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"] + "," + Amount + "";
                                }
                                else
                                {
                                    sAmount = Amount;
                                }
                            }
                            else
                            {
                                sProductID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"];
                                sProDetailID = string.IsNullOrEmpty(Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"]) ? "0" : Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"];
                                sAmount = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"];
                            }
                        }
                        else
                        {
                            int i = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"].IndexOf(ProductIDS);
                            if (i < 0)
                            {
                                //产品主表IDS
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"] != "")
                                {
                                    sProductID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"] + "," + ProductIDS;
                                }
                                else
                                {
                                    sProductID = ProductIDS;
                                }
                                //详细表IDS
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"] != "")
                                {
                                    sProDetailID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"] + "," + ProDetailIDS;
                                }
                                else
                                {
                                    sProDetailID = ProDetailIDS;
                                }
                                //数量
                                if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"] != "")
                                {
                                    sAmount = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"] + "," + Amount + "";
                                }
                                else
                                {
                                    sAmount = Amount;
                                }
                            }
                            else
                            {
                                sProductID = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProductID"];
                                sProDetailID = string.IsNullOrEmpty(Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"]) ? "0" : Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["ProDetailID"];
                                sAmount = Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"]["Amount"];
                            }
                        }
                    }
                    else
                    {
                        //购物车Cookies不存在
                        sProductID = ProductIDS;
                        sProDetailID = ProDetailIDS;
                        sAmount = Amount;
                    }
                    //写入Cookies
                    if (sProductID != "" && sAmount != "")
                    {
                        BLL.BLLShoppingCar cart = new BLL.BLLShoppingCar();
                        cart.WriteCookies(sProductID, sProDetailID, sAmount);
                    }
                    Response.Write("{\"error\":\"已添加到购物车。\",\"result\":\"1\"}");
                    Response.End();
                    #endregion
                    break;
                case "CheckLogin":
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserName"];
                    if (Cookie != null)
                    {
                        Response.Write("{\"CheckResult\":\"1\",\"MemberName\":\"" + EncryptUtils.DecodeCookies(Cookie.Value) + "\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"CheckResult\":\"0\"}");
                        Response.End();
                    }
                    break;
                case "AddToFav":
                    HttpCookie IsUserID = Request.Cookies["" + UIConfig.CookiesName + "UserID"];
                    if (IsUserID != null)
                    {
                        BLL.CNVP_Favorite fav = new CNVP_Favorite();
                        Model.CNVP_Users mUser = new BLL.CNVP_Users().GetSingle_CNVP_UsersById(Convert.ToInt32(EncryptUtils.DecodeCookies(IsUserID.Value)));
                        Model.CNVP_Favorite mFav = new Model.CNVP_Favorite();
                        string _productID = Request.QueryString["productID"];
                        if (string.IsNullOrEmpty(_productID) || !Public.IsNumber(_productID))
                        {
                            _productID = "0";
                        }
                        mFav.ProductID = Convert.ToInt32(_productID);
                        mFav.UsersID = Convert.ToInt32(mUser.ID);
                        fav.Add_CNVP_Favorite(mFav);
                        Response.Write("{\"CheckResult\":\"1\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"CheckResult\":\"0\"}");
                        Response.End();
                    }
                    break;
                default:
                    Response.Write("{\"error\":\"错误。\"}");
                    Response.End();
                    break;
            }
        }
    }
}